using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace DbView
{
	public class ProcessRunner
	{
		String m_ExeName;
		String m_Output;
		bool  m_Errored;

		public ProcessRunner(String exeName)
		{
			m_ExeName = exeName;
			m_Output = String.Empty;
			m_Errored = false;
		}
		public String Output { get {return m_Output; } }
		public bool LastProcessErrored { get {return m_Errored; } }

		public void Run(String parameters)
		{
			m_Output = String.Empty;
			m_Errored = false;
			if (m_ExeName != string.Empty)
			{
				StreamReader outputReader = null;
				StreamReader errorReader = null;

				try
				{

					//Create Process Start information
					ProcessStartInfo processStartInfo =	new ProcessStartInfo(m_ExeName, parameters);
					
					processStartInfo.ErrorDialog = false;
					processStartInfo.UseShellExecute = false;
					processStartInfo.RedirectStandardError = true;
					processStartInfo.RedirectStandardInput = true;
					processStartInfo.RedirectStandardOutput = true;
					
					processStartInfo.CreateNoWindow = true; // mine
					
					//Execute the process
					Process process = new Process();
					process.StartInfo = processStartInfo;
					bool processStarted = process.Start();
					if (processStarted)
					{
						//Get the output stream
						outputReader = process.StandardOutput;
						errorReader = process.StandardError;
						process.WaitForExit();

						//Display the result
						m_Output = Environment.NewLine + "==============" + Environment.NewLine;
						m_Output += outputReader.ReadToEnd();
						m_Output += errorReader.ReadToEnd();
						m_Output += Environment.NewLine;
					}
				}
				catch (Exception ex)
				{
					m_Output = ex.Message;
					m_Errored = true;
				}
				finally
				{
				if(outputReader != null)
				{
					outputReader.Close();
				}
				if(errorReader != null)
				{
					errorReader.Close();
				}
			}
			}
			else
			{
				m_Output = "No program";
			}
		}

		public void ShellExec(String parameters)
		{
			m_Output = String.Empty;
			m_Errored = false;

			if (m_ExeName != string.Empty)
			{
				try
				{

					//Create Process Start information
					ProcessStartInfo processStartInfo =	new ProcessStartInfo(m_ExeName, parameters);
					
					processStartInfo.ErrorDialog = false;
					processStartInfo.UseShellExecute = true;
					processStartInfo.RedirectStandardError = false;
					processStartInfo.RedirectStandardInput = false;
					processStartInfo.RedirectStandardOutput = false;
					
					processStartInfo.CreateNoWindow = false; // mine
					
					//Execute the process
					Process process = new Process();
					process.StartInfo = processStartInfo;
					bool processStarted = process.Start();
					if (processStarted)
					{
					}
				}
				catch (Exception ex)
				{
					m_Output = ex.Message;
					m_Errored = true;
				}
				finally
				{
				}
			}
			else
			{
				m_Output = "No program";
			}
			
		}

	}
}
